package mn.slide.labmed.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpConnection implements Runnable {
	public static ArrayList<String> zsortierung, ztitel, zint_titel,
			zkommentar, zfeld1, zfeld2, zfeld3, zfeld4, zfeld5, zfeld6, zfeld7,
			zfeld8, zflag1, zflag2, zflag3, zflag4, zflag5, zflag6, zflag7,
			zflag8, zbezug, zinfo, zbild, ztabelle, zpdf, zleistungskuerzel,
			zgeaendert, zdbid, zindikation, zansatztage, zbemerkung,
			zverknuepfung, zuntersuchung, ztemp, zunterordnung, zsynonym,
			zfeld9, zfeld_sort;
	public static ArrayList<String> parent_id, model_id, zkontaktepool_id_1,
			zkontaktepool_id_2, zduplikat, zfreigabe, zakkreditiert;
	public static HttpGet get;
	public static final int DID_START = 0;
	public static final int DID_ERROR = 1;
	public static final int DID_SUCCEED = 2;

	private static final int GET = 0;
	private static final int POST = 1;
	private static final int PUT = 2;
	private static final int DELETE = 3;
	private String url;
	private int method;
	private Handler handler;
	private String data;

	private HttpClient httpClient;

	public HttpConnection() {
		this(new Handler());
	}

	public HttpConnection(Handler h) {
		handler = h;
	}

	public void create(int method, String url, String data) {
		this.method = method;
		this.url = url;
		this.data = data;
		ConnectionManager.getInstance().push(this);
	}

	public void get(String url) {
		create(GET, url, null);
	}

	public void post(String url, String data) {
		create(POST, url, data);
	}

	public void put(String url, String data) {
		create(PUT, url, data);
	}

	public void delete(String url) {
		create(DELETE, url, null);
	}

	public void run() {
		handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
		httpClient = new DefaultHttpClient();
		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60000);
		try {
			HttpResponse response = null;
			switch (method) {
			case GET:
				 //String utfurl = URLEncoder.encode(url, "LATIN1");
				get = new HttpGet(url);
				response = httpClient.execute(get);
			
				break;
			case POST:
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new StringEntity(data));
				response = httpClient.execute(httpPost);
				break;
			case PUT:
				HttpPut httpPut = new HttpPut(url);
				httpPut.setEntity(new StringEntity(data));
				response = httpClient.execute(httpPut);
				break;
			case DELETE:
				response = httpClient.execute(new HttpDelete(url));
				break;
			}
			processEntity(response.getEntity());
		} catch (Exception e) {
			handler.sendMessage(Message.obtain(handler,
					HttpConnection.DID_ERROR, e));
		}
		ConnectionManager.getInstance().didComplete(this);
	}

	private void processEntity(HttpEntity entity) throws IllegalStateException,
			IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				entity.getContent(),"ISO-8859-16"));
		
		model_id = new ArrayList<String>();
		parent_id = new ArrayList<String>();
		zsortierung = new ArrayList<String>();
		ztitel = new ArrayList<String>();
		zint_titel = new ArrayList<String>();
		zkommentar = new ArrayList<String>();
		zfeld1 = new ArrayList<String>();
		zfeld2 = new ArrayList<String>();
		zfeld3 = new ArrayList<String>();
		zfeld4 = new ArrayList<String>();
		zfeld5 = new ArrayList<String>();
		zfeld6 = new ArrayList<String>();
		zfeld7 = new ArrayList<String>();
		zfeld8 = new ArrayList<String>();
		zfeld9 = new ArrayList<String>();
		zflag1 = new ArrayList<String>();
		zflag2 = new ArrayList<String>();
		zflag3 = new ArrayList<String>();
		zflag4 = new ArrayList<String>();
		zflag5 = new ArrayList<String>();
		zflag6 = new ArrayList<String>();
		zflag7 = new ArrayList<String>();
		zflag8 = new ArrayList<String>();
		zbezug = new ArrayList<String>();
		zinfo = new ArrayList<String>();
		zbild = new ArrayList<String>();
		ztabelle = new ArrayList<String>();
		zpdf = new ArrayList<String>();
		zleistungskuerzel = new ArrayList<String>();
		zfreigabe = new ArrayList<String>();
		zakkreditiert = new ArrayList<String>();
		zgeaendert = new ArrayList<String>();
		zdbid = new ArrayList<String>();
		zindikation = new ArrayList<String>();
		zansatztage = new ArrayList<String>();
		zbemerkung = new ArrayList<String>();
		zverknuepfung = new ArrayList<String>();
		zuntersuchung = new ArrayList<String>();
		zduplikat = new ArrayList<String>();
		ztemp = new ArrayList<String>();
		zunterordnung = new ArrayList<String>();
		zsynonym = new ArrayList<String>();
		zkontaktepool_id_1 = new ArrayList<String>();
		zkontaktepool_id_2 = new ArrayList<String>();
		zfeld_sort = new ArrayList<String>();

		JsonFactory jfactory = new JsonFactory();

		/*** read from file ***/
		JsonParser jParser = jfactory.createJsonParser(br);
		while (jParser.nextToken() != JsonToken.END_ARRAY) {

			String fieldname = jParser.getCurrentName();

			if ("id".equals(fieldname)) {
				jParser.nextToken();
				model_id.add(jParser.getText());
				Log.d("ene bol id", jParser.getText());
			}
			if ("parent_id".equals(fieldname)) {
				jParser.nextToken();
				parent_id.add(jParser.getText());
			}
			if ("sortierung".equals(fieldname)) {
				jParser.nextToken();
				zsortierung.add(jParser.getText());

			}
			if ("titel".equals(fieldname)) {
				jParser.nextToken();
				ztitel.add(jParser.getText());

			}

			if ("int_titel".equals(fieldname)) {
				jParser.nextToken();
				zint_titel.add(jParser.getText());

			}
			if ("kommentar".equals(fieldname)) {
				jParser.nextToken();
				zkommentar.add(jParser.getText());

			}
			if ("feld1".equals(fieldname)) {
				jParser.nextToken();
				zfeld1.add(jParser.getText());

			}
			if ("feld2".equals(fieldname)) {
				jParser.nextToken();
				zfeld2.add(jParser.getText());

			}
			if ("feld3".equals(fieldname)) {
				jParser.nextToken();
				zfeld3.add(jParser.getText());

			}
			if ("feld4".equals(fieldname)) {
				jParser.nextToken();
				zfeld4.add(jParser.getText());

			}
			if ("feld5".equals(fieldname)) {
				jParser.nextToken();
				zfeld5.add(jParser.getText());

			}
			if ("feld6".equals(fieldname)) {
				jParser.nextToken();
				zfeld6.add(jParser.getText());

			}
			if ("feld7".equals(fieldname)) {
				jParser.nextToken();
				zfeld7.add(jParser.getText());

			}
			if ("feld8".equals(fieldname)) {
				jParser.nextToken();
				zfeld8.add(jParser.getText());

			}
			if ("flag1".equals(fieldname)) {
				jParser.nextToken();
				zflag1.add(jParser.getText());

			}
			if ("flag2".equals(fieldname)) {
				jParser.nextToken();
				zflag2.add(jParser.getText());

			}
			if ("flag3".equals(fieldname)) {
				jParser.nextToken();
				zflag3.add(jParser.getText());

			}
			if ("flag4".equals(fieldname)) {
				jParser.nextToken();
				zflag4.add(jParser.getText());

			}
			if ("flag5".equals(fieldname)) {
				jParser.nextToken();
				zflag5.add(jParser.getText());

			}
			if ("flag6".equals(fieldname)) {
				jParser.nextToken();
				zflag6.add(jParser.getText());

			}
			if ("flag7".equals(fieldname)) {
				jParser.nextToken();
				zflag7.add(jParser.getText());

			}
			if ("flag8".equals(fieldname)) {
				jParser.nextToken();
				zflag8.add(jParser.getText());

			}
			if ("bezug".equals(fieldname)) {
				jParser.nextToken();
				zbezug.add(jParser.getText());

			}
			if ("info".equals(fieldname)) {
				jParser.nextToken();
				zinfo.add(jParser.getText());

			}
			if ("bild".equals(fieldname)) {
				jParser.nextToken();
				zbild.add(jParser.getText());

			}
			if ("tabelle".equals(fieldname)) {
				jParser.nextToken();
				ztabelle.add(jParser.getText());

			}
			if ("pdf".equals(fieldname)) {
				jParser.nextToken();
				zpdf.add(jParser.getText());

			}
			if ("leistungskuerzel".equals(fieldname)) {
				jParser.nextToken();
				zleistungskuerzel.add(jParser.getText());

			}
			if ("freigabe".equals(fieldname)) {
				jParser.nextToken();
				zfreigabe.add(jParser.getText());

			}
			if ("akkreditiert".equals(fieldname)) {
				jParser.nextToken();
				zakkreditiert.add(jParser.getText());

			}
			if ("geaendert".equals(fieldname)) {
				jParser.nextToken();
				zgeaendert.add(jParser.getText());

			}
			if ("dbid".equals(fieldname)) {
				jParser.nextToken();
				zdbid.add(jParser.getText());

			}
			if ("indikation".equals(fieldname)) {
				jParser.nextToken();
				zindikation.add(jParser.getText());

			}
			if ("ansatztage".equals(fieldname)) {
				jParser.nextToken();
				zansatztage.add(jParser.getText());

			}
			if ("bemerkung".equals(fieldname)) {
				jParser.nextToken();
				zbemerkung.add(jParser.getText());

			}
			if ("verknuepfung".equals(fieldname)) {
				jParser.nextToken();
				zverknuepfung.add(jParser.getText());

			}
			if ("untersuchung".equals(fieldname)) {
				jParser.nextToken();
				zuntersuchung.add(jParser.getText());

			}
			if ("duplikat".equals(fieldname)) {
				jParser.nextToken();
				zduplikat.add(jParser.getText());

			}
			if ("temp".equals(fieldname)) {
				jParser.nextToken();
				ztemp.add(jParser.getText());

			}
			if ("unterordnung".equals(fieldname)) {
				jParser.nextToken();
				zunterordnung.add(jParser.getText());

			}
			if ("synonym".equals(fieldname)) {
				jParser.nextToken();
				zsynonym.add(jParser.getText());

			}
			if ("feld9".equals(fieldname)) {
				jParser.nextToken();
				zfeld9.add(jParser.getText());

			}
			if ("kontaktepool_id_1".equals(fieldname)) {
				jParser.nextToken();

				zkontaktepool_id_1.add(jParser.getText());

			}
			if ("kontaktepool_id_2".equals(fieldname)) {
				jParser.nextToken();
				zkontaktepool_id_2.add(jParser.getText());

			}
			if ("feld_sort".equals(fieldname)) {
				jParser.nextToken();
				zfeld_sort.add(jParser.getText());

			}

		}
		jParser.close();
		Message message = Message.obtain(handler, DID_SUCCEED);
		handler.sendMessage(message);
	}
	public static void clear() {
		HttpConnection.model_id.clear();
		HttpConnection.parent_id.clear();
		HttpConnection.zsortierung.clear();
		HttpConnection.ztitel.clear();
		HttpConnection.zint_titel.clear();
		HttpConnection.zfeld1.clear();
		HttpConnection.zfeld2.clear();
		HttpConnection.zfeld3.clear();
		HttpConnection.zfeld4.clear();
		HttpConnection.zfeld5.clear();
		HttpConnection.zfeld6.clear();
		HttpConnection.zfeld7.clear();
		HttpConnection.zfeld8.clear();
		HttpConnection.zflag1.clear();
		HttpConnection.zflag2.clear();
		HttpConnection.zflag3.clear();
		HttpConnection.zflag4.clear();
		HttpConnection.zflag5.clear();
		HttpConnection.zflag6.clear();
		HttpConnection.zflag7.clear();
		HttpConnection.zflag8.clear();
		HttpConnection.zbezug.clear();
		HttpConnection.zinfo.clear();
		HttpConnection.zbild.clear();
		HttpConnection.ztabelle.clear();
		HttpConnection.zpdf.clear();
		HttpConnection.zleistungskuerzel.clear();
		HttpConnection.zfreigabe.clear();
		HttpConnection.zakkreditiert.clear();
		HttpConnection.zindikation.clear();
		HttpConnection.zgeaendert.clear();
		HttpConnection.zansatztage.clear();
		HttpConnection.zbemerkung.clear();
		HttpConnection.zverknuepfung.clear();
		HttpConnection.zuntersuchung.clear();
		HttpConnection.ztemp.clear();
		HttpConnection.zunterordnung.clear();
		HttpConnection.zsynonym.clear();
		HttpConnection.zduplikat.clear();
		HttpConnection.zkontaktepool_id_1.clear();
		HttpConnection.zkontaktepool_id_2.clear();
		HttpConnection.zfeld_sort.clear();
	}

}
